package com.pro.staffmanage.druid.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class MyDruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource getBeanDruid(){
        return new DruidDataSource();
    }

    //  注册ServletBean -- Druid监控视图StatViewServlet
    @Bean
    public ServletRegistrationBean regStatViewServlet(){
        ServletRegistrationBean<StatViewServlet> servletBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        servletBean.setInitParameters(new HashMap<String, String>(){
            {
                put("loginUsername","Freak");
                put("loginPassword","123456");
            }
        });

        return servletBean;
    }

    //  注册过滤器WebStatFilter
    @Bean
    public FilterRegistrationBean regWebStatFilter(){
        FilterRegistrationBean<WebStatFilter> filterBean = new FilterRegistrationBean<>(new WebStatFilter());

        filterBean.setInitParameters(new HashMap<String, String>(1){
            {
                put("exclusion","/js/**,/css/**,/img/**,/druid/**");
            }
        });

        return filterBean;
    }

}
